package com.fmdj.dr.service.impl;

import com.fmdj.common.exception.GlobalException;
import com.fmdj.dr.db.dao.WalletDao;
import com.fmdj.dr.db.dao.WalletIncomeDao;
import com.fmdj.dr.db.pojo.WalletIncomeEntity;
import com.fmdj.dr.service.WalletIncomeService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class WalletIncomeServiceImpl implements WalletIncomeService {
    @Autowired
    private WalletDao walletDao;

    @Autowired
    private WalletIncomeDao walletIncomeDao;

    @Override
    public int transfer(WalletIncomeEntity entity) {
        //插入转账记录
        int rows = walletIncomeDao.insert(entity);
        if (rows!=1) {
            throw new GlobalException("插入转账记录失败");
        }
        HashMap<Object, Object> map = new HashMap<>() {{
            put("driverId", entity.getDriverId());
            put("amount", entity.getAmount());
        }};
        rows = walletDao.updateWalletBalance(map);
        if (rows!=1) {
            throw new GlobalException("向司机钱包充值失败");
        }
        return rows;
    }
}
